package top.mcwebsite.playandroid.common.basic.impl

import android.app.Application
import kotlinx.coroutines.flow.Flow
import top.mcwebsite.playandroid.common.api.AppService
import top.mcwebsite.playandroid.common.basic.impl.settings.AppSettings
import top.mcwebsite.playandroid.lib.servicemanager.core.annoation.ServiceImpl

@ServiceImpl(AppService::class, isSingleton = true)
class AppServiceImpl : AppService {

    private var application: Application? = null

    override fun init(application: Application) {
        this.application = application
        // 异步初始化设置
        AppSettings.init(isSync = false)
    }


    override fun getApplication(): Application {
        return application ?: throw IllegalStateException("AppService 未初始化")
    }

    override fun darkMode(isDarkMode: Boolean) {
        AppSettings.setDarkMode(isDarkMode)
    }

    override fun followSystemTheme(isFollow: Boolean) {
        AppSettings.setFollowSystemTheme(isFollow)
    }

    override fun isDarkMode(): Boolean {
        return AppSettings.isDarkMode()
    }

    override fun isFollowSystemTheme(): Boolean {
        return AppSettings.isFollowSystemTheme()
    }

    override fun isDarkModeFlow(): Flow<Boolean?> {
        return AppSettings.isDarkModeFlow()
    }

    override fun isFollowSystemThemeFlow(): Flow<Boolean?> {
        return AppSettings.isFollowSystemThemeFlow()
    }

}